﻿using MyCourseManager.Common;
using MyCourseManager.DataAccess.DAL;
using MyCourseManager.DataAccess.DataModel;
using MyCourseManager.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MyCourseManager.ViewModel
{
    public class LoginViewModel:NotifyBase
    {
        /// <summary>
        /// 关闭窗口命令
        /// </summary>
        public CommondBase closewindowcommand {  get; set; }
        public LoginModel LoginModel { get; set; }
        public CommondBase LoginCommand { get; set; }

        private string _errorMsg;

        public string errorMsg
        {
            get { return _errorMsg; }
            set { _errorMsg = value;this.Notify(); }
        }

        /// <summary>
        /// 进度条
        /// </summary>
        private Visibility _progressBar=Visibility.Collapsed;

        public Visibility ProgressBar
        {
            get { return _progressBar; }
            set { _progressBar = value;this.Notify(); LoginCommand.RaiseExcute();  }
        }



        public LoginViewModel() { 
            this.LoginModel = new LoginModel();
            this.LoginModel.UserName = "aa123";
            this.LoginModel.Password = "123456";
            this.LoginModel.VerifyCode = "1234";
            this.closewindowcommand = new CommondBase();
            this.closewindowcommand.dosomthing = new Action<object>((o) => { 
                //关闭窗口
                (o as Window).Close();
            });
            this.closewindowcommand.cando = new Func<object, bool>((o) => { return true; });


            this.LoginCommand = new CommondBase();
            this.LoginCommand.dosomthing = new Action<object>(DoLogin);
            //进度条隐藏 登陆按钮也不可点击
            this.LoginCommand.cando = new Func<object, bool>((o) => { return ProgressBar==Visibility.Collapsed; });
        }

        private void DoLogin(object o)
        {
            this.ProgressBar = Visibility.Visible;

            if (string.IsNullOrEmpty(this.LoginModel.UserName))
            {
                errorMsg = "请输入用户名";
                this.ProgressBar = Visibility.Collapsed;
                return;
            }

            

            if (string.IsNullOrEmpty(this.LoginModel.Password))
            {
                errorMsg = "请输入密码";
                this.ProgressBar = Visibility.Collapsed;
                return;
            }
            if (string.IsNullOrEmpty(this.LoginModel.VerifyCode))
            {
                errorMsg = "请输入验证码";
                this.ProgressBar = Visibility.Collapsed;
                return;
            }

            if (this.LoginModel.VerifyCode != "1234")
            {
                errorMsg = "验证码输入错误，只能是1234";
                this.ProgressBar = Visibility.Collapsed;
                return;
            }


            

            Task.Run(new Action(() => {
                try
                {

                    UsersDAL dal = new UsersDAL();

                    List<DataAccess.DataModel.UsersInfo> usersInfos = dal.GetModelList("", "", "", null);
                    GlobalValues.UserInfo = dal.checkUserinfo(this.LoginModel.UserName, this.LoginModel.Password);

                    if (GlobalValues.UserInfo == default(UsersInfo))
                    {
                        throw new Exception("登录失败！用户名或密码错误！");
                    }

                    Application.Current.Dispatcher.BeginInvoke(new Action(() => {
                        this.errorMsg = "";
                        (o as Window).DialogResult = true;//控制APP.xaml.cs语句继续往下执行打开主窗口
                    }));
                }
                catch (Exception ex)
                {
                    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        errorMsg = ex.Message;
                    }));
                }
                finally {
                    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        this.ProgressBar = Visibility.Collapsed;
                    }));
                }
                
            }));


           
        }

    }
}
